package com.cn.por.auto.factory;

import java.util.ArrayList;
import java.util.List;

import com.cn.por.auto.dto.CommonComponent;
import com.cn.por.auto.dto.DataModel;
import com.cn.por.auto.dto.MapperDataModel;
import com.cn.por.auto.dto.MapperRelation;
import com.cn.por.auto.dto.ModelParam;
import com.cn.por.auto.entity.TableDetiles;
import com.cn.por.auto.enums.FunctionName;
import com.cn.por.util.freemarker.StringFormaterUtil;

public class MapperDataModelFactory implements DataModelFactory {

	private MapperDataModel mapperDataModel;
	
	public MapperDataModelFactory() {}

	@Override
	public void setProperties(ModelParam modelParam) {
		mapperDataModel = new MapperDataModel();
		mapperDataModel.setBasePackage(modelParam.getBasePackage());
		mapperDataModel.setTableName(modelParam.getTableName());
		/**
		 * 表名转换为Entity名称
		 */
		mapperDataModel.setEntityName(StringFormaterUtil.columnToProperty(mapperDataModel.getTableName()));
		/**
		 * 命名空间
		 */
		mapperDataModel
				.setNamespace(mapperDataModel.getBasePackage() + ".dao." + mapperDataModel.getEntityName() + "Dao");
		/**
		 * BaseResultMap 建立数据库字段与java对象映射关系
		 */
		List<MapperRelation> mapperRelationList = new ArrayList<MapperRelation>();
		for (TableDetiles column : modelParam.getTableColumn()) {
			MapperRelation mapperRe = new MapperRelation();
			mapperRe.setColumn(column.getColumnName().toUpperCase());
			mapperRe.setProperty(
					StringFormaterUtil.lowerCaptureName(StringFormaterUtil.columnToProperty(column.getColumnName())));
			mapperRelationList.add(mapperRe);
		}
		mapperDataModel.setMapperRelations(mapperRelationList);
		/**
		 * 表主键
		 */
		mapperDataModel.setPrimaryKey(modelParam.getPrimaryKey());

		/**
		 * 查询组建
		 */
		List<CommonComponent> selectComponentlist = new ArrayList<CommonComponent>();
		if (null != mapperDataModel.getPrimaryKey()) {
			String primaryKeyType = "java.lang.String";
			for (TableDetiles tc : modelParam.getTableColumn()) {
				if (tc.getColumnName().equals(mapperDataModel.getPrimaryKey())) {
					if ("INT".equals(tc.getColumnType())) {
						primaryKeyType = "java.lang.Integer";
					}
				}
			}
			CommonComponent comc = new CommonComponent();
			comc.setId(FunctionName.getById.name());
			comc.setParameterType(primaryKeyType);
			List<MapperRelation> conditions = new ArrayList<MapperRelation>();
			MapperRelation m1 = new MapperRelation();
			m1.setColumn(mapperDataModel.getPrimaryKey());
			m1.setProperty(StringFormaterUtil
					.lowerCaptureName(StringFormaterUtil.columnToProperty(mapperDataModel.getPrimaryKey())));
			conditions.add(m1);
			comc.setConditions(conditions);
			selectComponentlist.add(comc);
		}
		/**
		 * getList
		 */
		CommonComponent getlist = new CommonComponent();
		getlist.setId(FunctionName.getList.name());
		getlist.setParameterType(mapperDataModel.getBasePackage() + ".entity." + mapperDataModel.getEntityName());
		getlist.setResultType(mapperDataModel.getBasePackage() + ".entity." + mapperDataModel.getEntityName());
		List<MapperRelation> conditions = new ArrayList<MapperRelation>();
		for (TableDetiles condition : modelParam.getConditionColumn()) {
			for (MapperRelation mr : mapperDataModel.getMapperRelations()) {
				if (condition.getColumnName().equals(mr.getColumn())) {
					MapperRelation m1 = new MapperRelation();
					m1.setColumn(mr.getColumn());
					m1.setProperty(mr.getProperty());
					conditions.add(m1);
				}
			}
		}
		getlist.setConditions(conditions);
		selectComponentlist.add(getlist);
		mapperDataModel.setSelectComponents(selectComponentlist);
		/**
		 * 唯一主键属性名
		 */
		mapperDataModel.setPrimaryName(StringFormaterUtil
				.lowerCaptureName(StringFormaterUtil.columnToProperty(mapperDataModel.getPrimaryKey())));
		/**
		 * 更新组建
		 */
		CommonComponent up = new CommonComponent();
		up.setId(FunctionName.updateById.name());
		up.setParameterType(mapperDataModel.getBasePackage() + ".entity." + mapperDataModel.getEntityName());
		mapperDataModel.setUpdateComponents(up);
		/**
		 * 新增组建
		 */
		CommonComponent add = new CommonComponent();
		add.setId(FunctionName.addOne.name());
		add.setParameterType(mapperDataModel.getBasePackage() + ".entity." + mapperDataModel.getEntityName());
		mapperDataModel.setAddComponents(add);
		mapperDataModel.setFtlName(modelParam.getFtlName());
		mapperDataModel.setFilePath("../tps-por-db/src/main/resources/sql/"
				+ StringFormaterUtil.lowerCaptureName(mapperDataModel.getEntityName()) + "/");
		mapperDataModel.setFileName(modelParam.getFileName());
		mapperDataModel.setFolder("sql/"+modelParam.getFileName().replace(".xml", ""));
	}

	@Override
	public DataModel getDataModel() {
		return this.mapperDataModel;
	}

}
